package com.example.demoone

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.core.graphics.toColorInt
import com.example.day01.R

class ColorAdapter : RecyclerView.Adapter<ColorAdapter.PageViewHolder>() {

    // 在下面的数据类里面增加图片和文字描述字段

    data class PageData(val color: String, val text: String, val img: Int, val imgText: String)

    // 页面数据：颜色 + 文本
    private val pages = listOf(
        PageData("#FF5733", "Page 1", R.drawable.sh, "Image Text 1"),
        PageData("#33FF57", "Page 2", R.drawable.bj, "Image Text 2"),
        PageData("#3357FF", "Page 3", R.drawable.sz, "Image Text 3")
    )

    inner class PageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.pageText)
        val imageView: ImageView = itemView.findViewById(R.id.img8)
        val textView2: TextView = itemView.findViewById(R.id.pageText4)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_page, parent, false)






        return PageViewHolder(view)
    }


    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        val page = pages[position]
        with(holder) {
            textView.text = page.text
            imageView.setImageResource(page.img)
            textView2.text = page.imgText
            itemView.setBackgroundColor(page.color.toColorInt())
        }
    }

    override fun getItemCount() = pages.size

}